﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Uninstaller
{
    class Program
    {
        static void Main(string[] args)
        {
            string mP = "";
            bool uMabi = false;
            int id = -1;
            try
            {
                mP = args[0];
                uMabi = bool.Parse(args[1]);
                id = int.Parse(args[2]);
                Environment.CurrentDirectory = mP;
            }
            catch
            {
                Console.WriteLine("You can't run this program manually!");
                Environment.Exit(0);
            }

            Console.WriteLine("Waiting for PowerPatcher (" + id + ") to exit...");

            try
            {
                while (System.Diagnostics.Process.GetProcessById(id) != null)
                    System.Threading.Thread.Sleep(33);
            }
            catch { }

            Console.WriteLine("Powerpatcher has exited.");

            try
            {
                //TODO: Extract/download nexon's launcher if the backup is missing.
                if (!File.Exists("Mabinogi.ppbackup"))
                {
                    Console.WriteLine("Uh oh! We can't seem to find the Nexon launcher (Mabinogi.ppbackup).... this means you should get the stock patcher somewhere after the uninstall has finished! (Or, you can hit the big red X right now and keep PowerPatcher as your launcher) {0}\r\n\r\nPress any key to acknowlege this message",
                        ((uMabi) ? "In addition, you selected to also uninstall Mabinogi. This requires Nexon's launcher, which can't be found, so Mabinogi will not be uninstalled." : ""));
                    uMabi = false;
                    Console.ReadKey(true);
                    File.Delete("Mabinogi.exe");
                }
                else
                {
                    File.Delete("Mabinogi.exe");
                    File.Move("Mabinogi.ppbackup", "Mabinogi.exe");
                }
                Console.WriteLine("Beginning uninstall...");
                Console.WriteLine("Enumerating PP files...");
                string[] files = Directory.GetFiles("Extentions", "*.*", SearchOption.AllDirectories);
                foreach (string f in files)
                {
                    Console.Write("Deleting " + f + " ...");
                    File.Delete(f);
                    Console.WriteLine("Done");
                }
                Console.Write("Removing folders...");
                Directory.Delete("Extentions", true);
                Console.WriteLine("Done");
                Console.Write("PowerPatcher uninstall finished. Press any key to ");
                if (uMabi)
                {
                    Console.WriteLine("begin uninstalling Mabinogi.");
                    Console.ReadKey(true);
                    System.Diagnostics.Process.Start("Mabinogi.exe", "/uninstall");
                }
                else
                {
                    Console.WriteLine("exit the uninstaller.");
                    Console.ReadKey(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.ToString());
                Console.ReadKey();
            }
        } 
    }
}
